include $(top_dir)/helpers.mk

src = $(wildcard **/*_test.c)
target ::= $(addprefix $(build_test_dir), $(basename $(src)))
build_dir = $(call uniq,$(dir $(target)))

CFLAGS += -I$(cmoka_include) -I$(bot_include)
LDFLAGS :=
LDLIBS := -L$(cmoka_lib_path) -lcmocka -L$(bot_lib_path) -lscrapper
DYNAMIC_LIBRARY_PATH := LD_LIBRARY_PATH=$(cmoka_lib_path):$(bot_lib_path)

all: $(build_dir) $(target) run_test

$(build_dir):
	@mkdir -p $(build_dir)

-include $(addsuffix .d, $(target))

$(target): $(bot_shared_lib)

$(target): $(build_test_dir)%: %.c
	@$(call print_info,building test)
	$(CC) -o $@ $< $(CFLAGS) $(LDLIBS)

run_test:
	@$(call print_info,running test)
	$(if $(test_suite), \
		$(foreach file, \
			$(foreach _, $(target), $(if $(findstring $(test_suite), $(notdir $_)), $_)), \
			$(DYNAMIC_LIBRARY_PATH) $(file)), \
		$(foreach file, $(target), $(DYNAMIC_LIBRARY_PATH) $(file)))

clean:
	rm -fr $(build_test_dir)

.PHONY: clean run_test
